package com.zyl.ag.manager;  
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.zyl.ag.bean.Account;
import com.zyl.util.DBHelper;

/**
 * 题库
 * @param  
 * @return
 */
public class AccountManager {  
      
        private DBHelper helper;  
     
        
        
        public AccountManager(Context context) {  
            helper = new DBHelper(context);//初始化数据库  
         
        }  
      
        
    	private Account rowMapper(Cursor cursor) {
    		Integer id = cursor.getInt(	cursor.getColumnIndex("_id"));
    		String name = cursor.getString(	cursor.getColumnIndex("name"));
    		Integer course_id = cursor.getInt(cursor.getColumnIndex("course_id"));
    		Integer downloadcount = cursor.getInt(cursor.getColumnIndex("downloadcount"));
    		Integer sort_order = cursor.getInt(cursor.getColumnIndex("sort_order"));
    		Integer type = cursor.getInt(cursor.getColumnIndex("type"));
    		Integer status = cursor.getInt(cursor.getColumnIndex("status"));
    		Integer favor = cursor.getInt(cursor.getColumnIndex("favor"));
    		String photo = cursor.getString(cursor.getColumnIndex("photo"));
    		String url = cursor.getString(cursor.getColumnIndex("url"));

    		
    		Account l =new Account();	
    		l.set_id(id);
    	 
    		return l;
    	}
        
        
        
        
        
        
        
        
        
        
        
        
        /**  
         * 插入一个Account  
         * @param p 要插入的Account  
         */  
        public void insert(Account a) {  
            SQLiteDatabase db = helper.getWritableDatabase();  //获取到数据库  
            db.execSQL("INSERT INTO account(name,money) VALUES(?,?)", new Object[] {a.getName(),a.getMoney()  });  
            db.close();  
        }  
      
        /**  
         * 根据ID删除  
         * @param id 要删除的Account的ID  
         */  
        public void delete(Integer id) {  
            SQLiteDatabase db = helper.getWritableDatabase();  
            db.execSQL("DELETE FROM Account WHERE id=?", new Object[] { id });  
            db.close();  
        }  
      
        public void reduceAppearTimes(Integer qid) {  
            SQLiteDatabase db = helper.getWritableDatabase();  
             db.execSQL("UPDATE Account SET  appearTimes=appearTimes-1 WHERE id=?", new Object[] {  qid });  
            db.close();  
        }  
        public void addAppearTimes(Integer qid) {  
        	SQLiteDatabase db = helper.getWritableDatabase();  
        	db.execSQL("UPDATE Account SET  appearTimes=appearTimes+1 WHERE id=?", new Object[] {  qid });  
        	db.close();  
        }  
        /**  
         * 
         * 这里主要是分组 逻辑 和判断 是否 是完结 最后一位的逻辑
         * 根据ID查找  
         * @param Integer id,Integer page,Integer size
         * @return 对应的对象, 如果未找到返回null  
         */  
        
        public Account nextAccount(Integer id ) {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT title,answer,appearTimes,id FROM Account WHERE id>? and  appearTimes>0  order by id asc limit 1", new String[] { id.toString() });  
            Account q = null;  
            if (cursor.moveToNext()) {  
                String title = cursor.getString(0);  
                String answer = cursor.getString(1);  
                Integer appearTimes = cursor.getInt(2);  
                Integer qid = cursor.getInt(3);  
                q = new Account();  
            }  
            cursor.close();  
            db.close();  
            return q;  
        }  
        
        
        public Account randomAccount( ) {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT title,answer,appearTimes,id FROM Account WHERE   appearTimes>0  order by id asc limit 1", null);  
            Account q = null;  
            if (cursor.moveToNext()) {  
                String title = cursor.getString(0);  
                String answer = cursor.getString(1);  
                Integer appearTimes = cursor.getInt(2);  
                Integer qid = cursor.getInt(3);  
                q = new Account();  
            }  
            cursor.close();  
            db.close();  
            return q;  
        }  
        
        /**  
         * 查询所有Account对象  
         * @return Account对象集合, 如未找到, 返回一个size()为0的List  
         */  
        public List<Account> findAll() {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT id,title,answer,appearTimes FROM Account", null);  
            List<Account> Accounts = new ArrayList<Account>();  
            while (cursor.moveToNext()) {  
                Integer id = cursor.getInt(0);  
                String title = cursor.getString(1);  
                String answer = cursor.getString(2);  
                Integer appearTimes = cursor.getInt(3);  
                Accounts.add(new Account());  
            }  
            cursor.close();  
            db.close();  
            return Accounts;  
        }  
   
  
        /**  
         * 查询某一页数据  
         * @param page 页码  
         * @param size 每页记录数  
         * @return  
         */  
        public List<Account> findPage(int page, int size) {  
            SQLiteDatabase db = helper.getReadableDatabase();  
            Cursor cursor = db.rawQuery("SELECT id,title,answer,appearTimes FROM Account where appearTimes>0 LIMIT ?,?" //  
                    , new String[] { String.valueOf((page - 1) * size), String.valueOf(size) });  
            List<Account> Accounts = new ArrayList<Account>();  
            while (cursor.moveToNext()) {  
                Integer id = cursor.getInt(0);  
                String title = cursor.getString(1);  
                String answer = cursor.getString(2);  
                Integer appearTimes = cursor.getInt(3);  
                Accounts.add(new Account());  
            }  
            cursor.close();  
            db.close();  
            return Accounts;  
        }  
        /**  
         * 查询某一页数据  
         * @param page 页码  
         * @param size 每页记录数  
         * @return  
         */  
        public int findPageCount(int page, int size) {  
        	SQLiteDatabase db = helper.getReadableDatabase();  
        	int count =0;
        	Cursor cursor = db.rawQuery("SELECT ID FROM Account where appearTimes>0 and id>? and id<?" //  
        			, new String[] { String.valueOf((page - 1)*size), String.valueOf(page*size) });  
        	count = cursor.getCount();  
        	cursor.close();  
        	db.close();  
        	return count;  
        }  

}  

